package com.wellsoft.mobileoa.utils.action;

import java.util.ArrayList;
import java.util.List;

import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.BaseWidget;
import com.wellsoft.mobileoa.utils.SelectActionDataAdapter;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_ActionData;
import com.wmobile.utils.PopListDialog;
import com.wmobile.utils.PopOpinionDialog;
import com.wmobile.utils.ServiceResponse;

public class PutForwardOpinionAction extends BaseAction {

	public PutForwardOpinionAction(WSOA_Action action, BaseView baseView) {
		this.action = action;
		this.baseView = baseView;
	}

	@Override
	public ServiceResponse excuteAction() {
		PopOpinionDialog popDialog;
		List<Integer> selectedIndex;
		BaseWidget sender;
		BaseWidget hiddenSender;
		String resultValue = "";
		String hiddenResultValue = "";

		selectedIndex = new ArrayList<Integer>();
		sender = baseView.getWidgetById(action.getPname());
		if (!SystemConfig.stringIsNullOrEmpty(action.getChangeNotedite())
				&& action.getChangeNotedite().equals("1")) {
			if (sender.hasEdited) {
				return new ServiceResponse(true);
			}
		}
		String senderValue = "";
		if (sender != null && sender.getRawValue() != null
				&& !sender.getRawValue().toUpperCase().equals("FULL")) {
			senderValue = sender.getRawValue();
		}
		hiddenSender = baseView.getWidgetById(action.getPhname());
		if (hiddenSender != null
				&& !SystemConfig
						.stringIsNullOrEmpty(hiddenSender.getRawValue())) {
			for (int i = 0; i < action.getActionDataSet().getActionDatas()
					.size(); i++) {
				if (action.getActionDataSet().getActionDatas().get(i)
						.getValue().equals(hiddenSender.getRawValue())) {
					selectedIndex.add(Integer.valueOf(i));
					break;
				}
			}
		}
		popDialog = new PopOpinionDialog<WSOA_ActionData>(
				baseView.getBaseActivity(), action.getPageTitle().getMessage(),
				selectedIndex, action.getActionDataSet(), senderValue);
		if (popDialog.showDialog() != PopListDialog.DialogResultOK) {
			return new ServiceResponse(false);
		}
		selectedIndex = popDialog.getSelectedIndex();
		if (!SystemConfig.stringIsNullOrEmpty(popDialog.getOpinion())
				|| selectedIndex.size() > 0) {
			if (hiddenSender != null) {
				for (Integer i : selectedIndex) {
					if (!SystemConfig.stringIsNullOrEmpty(hiddenSender
							.getField().getSeparator())) {
						hiddenResultValue += action.getActionDataSet()
								.getActionDatas().get(i.intValue()).getValue()
								+ hiddenSender.getField().getSeparator();
					}
				}
				if (!SystemConfig.stringIsNullOrEmpty(hiddenResultValue)) {
					hiddenResultValue = hiddenResultValue.substring(0,
							hiddenResultValue.length()
									- hiddenSender.getField().getSeparator()
											.length());
				}
				hiddenSender.setValue(hiddenResultValue);
			}
			sender.setValue(popDialog.getOpinion());

			return new ServiceResponse(true);
		} else {
			return new ServiceResponse(false);
		}
	}
}
